home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / WINGs / wmisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-22  |  7.1 KB  |  340 lines

  1.  
  2. #include "WINGsP.h"
  3.  
  4. #include <wraster.h>
  5. #include <ctype.h>
  6.  
  7.  
  8. void
  9. W_DrawRelief(W_Screen *scr, Drawable d, int x, int y, unsigned int width,
  10.          unsigned int height, WMReliefType relief)
  11. {
  12.     Display *dpy = scr->display;
  13.     GC bgc;
  14.     GC wgc;
  15.     GC lgc;
  16.     GC dgc;
  17.  
  18.     switch (relief) {
  19.      case WRSimple:
  20.     XDrawRectangle(dpy, d, WMColorGC(scr->black), x, y, width-1, height-1);
  21.     return;
  22.     break;
  23.     
  24.      case WRRaised:
  25.     bgc = WMColorGC(scr->black);
  26.     dgc = WMColorGC(scr->darkGray);
  27.     wgc = WMColorGC(scr->white);
  28.     lgc = WMColorGC(scr->gray);
  29.     break;
  30.     
  31.      case WRSunken:
  32.     wgc = WMColorGC(scr->darkGray);
  33.     lgc = WMColorGC(scr->black);
  34.     bgc = WMColorGC(scr->white);
  35.     dgc = WMColorGC(scr->gray);
  36.     break;
  37.     
  38.      case WRPushed:
  39.     lgc = wgc = WMColorGC(scr->black);
  40.     dgc = bgc = WMColorGC(scr->white);
  41.     break;
  42.     
  43.      case WRRidge:
  44.     lgc = bgc = WMColorGC(scr->darkGray);
  45.     dgc = wgc = WMColorGC(scr->white);
  46.     break;
  47.  
  48.      case WRGroove:
  49.     wgc = dgc = WMColorGC(scr->darkGray);
  50.     lgc = bgc = WMColorGC(scr->white);
  51.     break;
  52.     
  53.      default:
  54.     return;
  55.     }
  56.     /* top left */
  57.     XDrawLine(dpy, d, wgc, x, y, x+width-1, y);
  58.     if (width > 2 && relief != WRRaised && relief!=WRPushed) {
  59.     XDrawLine(dpy, d, lgc, x+1, y+1, x+width-3, y+1);
  60.     }
  61.     
  62.     XDrawLine(dpy, d, wgc, x, y, x, y+height-1);
  63.     if (height > 2 && relief != WRRaised && relief!=WRPushed) {
  64.     XDrawLine(dpy, d, lgc, x+1, y+1, x+1, y+height-3);
  65.     }
  66.     
  67.     /* bottom right */
  68.     XDrawLine(dpy, d, bgc, x, y+height-1, x+width-1, y+height-1);
  69.     if (width > 2 && relief!=WRPushed) {
  70.     XDrawLine(dpy, d, dgc, x+1, y+height-2, x+width-2, y+height-2);
  71.     }
  72.  
  73.     XDrawLine(dpy, d, bgc, x+width-1, y, x+width-1, y+height-1);
  74.     if (height > 2 && relief!=WRPushed) {
  75.     XDrawLine(dpy, d, dgc, x+width-2, y+1, x+width-2, y+height-2);
  76.     }
  77. }
  78.  
  79.  
  80. static int
  81. fitText(char *text, WMFont *font, int width, int wrap)
  82. {
  83.     int i, j;
  84.     int w;
  85.  
  86.     if (text[0]==0)
  87.     return 0;
  88.     i = 0;
  89.     if (wrap) {
  90.     do {
  91.         i++;
  92.         w = WMWidthOfString(font, text, i);
  93.     } while (w < width && text[i]!='\n' && text[i]!=0);
  94.  
  95.     /* keep words complete */
  96.     if (!isspace(text[i])) {
  97.         j = i;
  98.         while (j>1 && !isspace(text[j]) && text[j]!=0)
  99.         j--;
  100.         if (j>1)
  101.         i = j;
  102.     }
  103.     } else {
  104.     while (text[i]!='\n' && text[i]!=0)
  105.         i++;
  106.     }
  107.  
  108.     return i;
  109. }
  110.  
  111.  
  112. int
  113. W_GetTextHeight(WMFont *font, char *text, int width, int wrap)
  114. {
  115.     char *ptr = text;
  116.     int count;
  117.     int length = strlen(text);
  118.     int h;
  119.     int fheight = WMFontHeight(font);
  120.  
  121.     h = 0;
  122.     while (length > 0) {
  123.     count = fitText(ptr, font, width, wrap);
  124.  
  125.     h += fheight;
  126.  
  127.     if (isspace(ptr[count]))
  128.         count++;
  129.  
  130.     ptr += count;
  131.     length -= count;
  132.     }
  133.     return h;
  134. }
  135.  
  136.  
  137. void
  138. W_PaintText(W_View *view, Drawable d, WMFont *font,  int x, int y,
  139.         int width, WMAlignment alignment, GC gc,
  140.         int wrap, char *text, int length)
  141. {
  142.     char *ptr = text;
  143.     int line_width;
  144.     int line_x;
  145.     int count;
  146.     int fheight = WMFontHeight(font);
  147.  
  148.     while (length > 0) {
  149.     count = fitText(ptr, font, width, wrap);
  150.  
  151.     line_width = WMWidthOfString(font, ptr, count);
  152.     if (alignment==WALeft)
  153.         line_x = x;
  154.     else if (alignment==WARight)
  155.         line_x = x + width - line_width;
  156.     else
  157.         line_x = x + (width - line_width) / 2;
  158.  
  159.     WMDrawString(view->screen, d, gc, font, line_x, y, ptr, count);
  160.  
  161.     y += fheight;
  162.  
  163.     if (isspace(ptr[count]))
  164.         count++;
  165.     
  166.     ptr += count;
  167.     length -= count;
  168.     }
  169. }
  170.  
  171.  
  172. void
  173. W_PaintTextAndImage(W_View *view, int wrap, GC textGC, W_Font *font,
  174.             WMReliefType relief, char *text,
  175.             WMAlignment alignment,  W_Pixmap *image,
  176.             WMImagePosition position, GC backGC, int ofs)
  177. {
  178.     W_Screen *screen = view->screen;
  179.     int ix, iy;
  180.     int x, y, w, h;
  181.     Drawable d = view->window;
  182.  
  183.     
  184. #ifdef DOUBLE_BUFFER
  185.     d = XCreatePixmap(screen->display, view->window, 
  186.               view->size.width, view->size.height, screen->depth);
  187. #endif
  188.     
  189.     /* background */
  190. #ifndef DOUBLE_BUFFER
  191.     if (backGC) {
  192.     XFillRectangle(screen->display, d, backGC,
  193.                0, 0, view->size.width, view->size.height);
  194.     } else {
  195.     XClearWindow(screen->display, d);
  196.     }
  197. #else
  198.     if (backGC)
  199.     XFillRectangle(screen->display, d, backGC, 0, 0,
  200.                view->size.width, view->size.height);
  201.     else {
  202.     XSetForeground(screen->display, screen->copyGC, 
  203.                view->attribs.background_pixel);
  204.     XFillRectangle(screen->display, d, screen->copyGC, 0, 0,
  205.                view->size.width, view->size.height);
  206.     }
  207. #endif
  208.  
  209.     
  210.     if (relief == WRFlat) {
  211.     x = 0;
  212.     y = 0;
  213.     w = view->size.width;
  214.     h = view->size.height;
  215.     } else {
  216.     x = 2;
  217.     y = 2;
  218.     w = view->size.width - 4;
  219.     h = view->size.height - 4;    
  220.     }
  221.  
  222.     /* calc. image alignment */
  223.     if (position!=WIPNoImage && image!=NULL) {
  224.     switch (position) {
  225.      case WIPOverlaps:
  226.      case WIPImageOnly:
  227.         ix = (view->size.width - image->width) / 2;
  228.         iy = (view->size.height - image->height) / 2;
  229.         /*
  230.         x = 2;
  231.         y = 0;
  232.          */
  233.         break;
  234.         
  235.      case WIPLeft:
  236.         ix = x;
  237.         iy = y + (h - image->height) / 2;
  238.         x = x + image->width + 5;
  239.         y = 0;
  240.         w -= image->width + 5;
  241.         break;
  242.         
  243.      case WIPRight:
  244.         ix = view->size.width - image->width - x;
  245.         iy = y + (h - image->height) / 2;
  246.         w -= image->width + 5;
  247.         break;
  248.         
  249.      case WIPBelow:
  250.         ix = (view->size.width - image->width) / 2;
  251.         iy = h - image->height;
  252.         y = 0;
  253.         h -= image->height;
  254.         break;
  255.         
  256.      default:
  257.      case WIPAbove:
  258.         ix = (view->size.width - image->width) / 2;
  259.         iy = y;
  260.         y = image->height;
  261.         h -= image->height;
  262.         break;
  263.     }
  264.     
  265.     ix += ofs;
  266.     iy += ofs;
  267.  
  268.     XSetClipOrigin(screen->display, screen->clipGC, ix, iy);
  269.     XSetClipMask(screen->display, screen->clipGC, image->mask);
  270.  
  271.     if (image->depth==1)
  272.         XCopyPlane(screen->display, image->pixmap, d, screen->clipGC,
  273.                0, 0, image->width, image->height, ix, iy, 1);
  274.     else
  275.         XCopyArea(screen->display, image->pixmap, d, screen->clipGC,
  276.               0, 0, image->width, image->height, ix, iy);
  277.     }
  278.  
  279.     /* draw text */
  280.     if (position != WIPImageOnly && text!=NULL) {
  281.     int textHeight;
  282.     
  283.     textHeight = W_GetTextHeight(font, text, w-8, wrap);
  284.     W_PaintText(view, d, font, x+ofs+4, y+ofs + (h-textHeight)/2, w-8,
  285.             alignment, textGC, wrap, text, strlen(text));
  286.     }
  287.     
  288.     
  289.     /* draw relief */
  290.     W_DrawRelief(screen, d, 0, 0, view->size.width, view->size.height, relief);
  291.     
  292. #ifdef DOUBLE_BUFFER
  293.     XCopyArea(screen->display, d, view->window, screen->copyGC, 0, 0,
  294.           view->size.width, view->size.height, 0, 0);
  295.     XFreePixmap(screen->display, d);
  296. #endif
  297. }
  298.  
  299.  
  300.  
  301. WMRange 
  302. wmkrange(int start, int count)
  303. {
  304.     WMRange range;
  305.     
  306.     range.position = start;
  307.     range.count = count;
  308.     
  309.     return range;
  310. }
  311.  
  312.  
  313. WMPoint 
  314. wmkpoint(int x, int y)
  315. {
  316.     WMPoint point;
  317.     
  318.     point.x = x;
  319.     point.y = y;
  320.     
  321.     return point;
  322. }
  323.  
  324.  
  325. WMSize 
  326. wmksize(unsigned int width, unsigned int height)
  327. {
  328.     WMSize size;
  329.     
  330.     size.width = width;
  331.     size.height = height;
  332.     
  333.     return size;
  334. }
  335.  
  336.  
  337.  
  338.  
  339.  
  340.